<?php

/**
 * This is the model class for table "tbl_instrument_elem".
 *
 * The followings are the available columns in table 'tbl_instrument_elem':
 * @property integer $elem_id
 * @property integer $instrument_id
 * @property string $asset_number
 * @property integer $current_user
 * @property integer $frequency
 * @property integer $usedtime
 * @property integer $status
 *
 * The followings are the available model relations:
 * @property Instrument $instrument
 */
class InstrumentElem extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return InstrumentElem the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'tbl_instrument_elem';
	}

    const STATUS_ACTIVE = 1;
    const STATUS_DEACTIVE = 0;

    public function getEnabledLable()
    {
        return $this->status == self::STATUS_ACTIVE ? '可预约' : '不可预约';
    }
	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('instrument_id, asset_number', 'required'),
			array('instrument_id, current_user, frequency, usedtime, status', 'numerical', 'integerOnly'=>true),
			array('asset_number', 'length', 'max'=>50),
			array('asset_number', 'unique'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('elem_id, instrument_id, asset_number, current_user, frequency, usedtime, status', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'instrument' => array(self::BELONGS_TO, 'Instrument', 'instrument_id'),
            'instrumentReserves'=>array(self::HAS_MANY,'InstrumentReserve','elem_id','condition'=>'status = '.InstrumentReserve::STATUS_PASS_EXAMINE),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'elem_id' => 'Elem',
			'instrument_id' => 'Instrument',
			'asset_number' => '资产编号',
			'current_user' => '当前用户',
			'frequency' => '使用总次数',
			'usedtime' => '使用总时长',
			'status' => '状态',
		);
	}
    /**
     * 获得$date时间缎内空闲的机器
     * @author : nonglei
     * @date   : 18/08/2014
     */
    public function getIdleMachines($date, $iid)
    {
        $model = Instrument::model()->findByPk($iid);
        $reserveList = $model->getreservelist();
        $elemList = $model->instrumentElems;
        $alreadyUseElemList = array();
        foreach($reserveList as $elem)
        {
            $eq1 = CDateTimeParser::parse($elem->start_date,'yyyy-MM-dd hh:mm:ss');
		    $eq2 = CDateTimeParser::parse($date,'yyyy-MM-dd hh:mm:ss');
            if($elem->instrumentElem->instrument_id == $iid && $eq2 == $eq1)
            {
                array_push($alreadyUseElemList, $elem->instrumentElem->elem_id);
            }
        }
        foreach($elemList as $elem)
        {
            if(!in_array($elem->elem_id, $alreadyUseElemList))
            {
                return $elem;
            }
        }
        return null;
    }
	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search($iid)
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('elem_id',$this->elem_id);
		$criteria->compare('instrument_id',$iid);
		$criteria->compare('asset_number',$this->asset_number,true);
		$criteria->compare('current_user',$this->current_user);
		$criteria->compare('frequency',$this->frequency);
		$criteria->compare('usedtime',$this->usedtime);
		$criteria->compare('status',$this->status);
		$criteria->order = 'elem_id DESC';

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
}
